package com.manage.project.controller;

import com.manage.project.bean.vo.FileUrl;
import com.manage.project.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Api(value = "minio上传", tags = {"minio"})
@RestController
@RequestMapping("minio")
@CrossOrigin
public class MinioController {

    @Resource
    private MinioService minioService;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////公共桶

    @PostMapping("/bucket/createcBucket")
    @ApiOperation(value = "【桶】创建桶", notes = "创建桶")
    public Boolean createPublicBucket(@RequestParam("bucketName") String bucketName) {
        return minioService.createBucket(bucketName);
    }

    @DeleteMapping("/bucket/deleteBucket")
    @ApiOperation(value = "【桶】删除桶", notes = "删除桶")
    public Boolean deleteBucket(@RequestParam("bucketName") String bucketName) {
        return minioService.deleteBucket(bucketName);
    }

    @GetMapping("/bucket/isBucketExists")
    @ApiOperation(value = "【桶】桶是否存在", notes = "桶是否存在")
    public Boolean bucketExists(@RequestParam("bucketName") String bucketName) {
        return minioService.bucketExists(bucketName);
    }

    @GetMapping("/bucket/listBucketNames")
    @ApiOperation(value = "【桶】桶列表", notes = "桶列表")
    public List<String> listBucket() {
        return minioService.listBucketNames();
    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////文件操作

    @PostMapping("/file/uploadFile")
    @ApiOperation(value = "【文件】上传文件", notes = "上传文件")
    public FileUrl uploadFile(@RequestParam("file") MultipartFile file,
                              @RequestParam @ApiParam(value = "桶名称") String bucketName) {
        return minioService.uploadFile(file, bucketName);
    }

    @DeleteMapping("/file/deleteFile")
    @ApiOperation(value = "【文件】删除文件", notes = "删除文件")
    public Boolean deleteFile(@RequestParam("bucketName") String bucketName,
                              @RequestParam("fileName") String fileName) {
        return minioService.deleteFile(bucketName, fileName);
    }

    @DeleteMapping("/file/deleteFiles")
    @ApiOperation(value = "【文件】批量删除文件", notes = "批量删除文件")
    public Boolean deleteFiles(@RequestParam("bucketName") String bucketName,
                               @RequestParam("fileNames") String... fileNames) {
        return minioService.deleteFiles(bucketName, Arrays.asList(fileNames));
    }

//    @GetMapping("/file/listFile")
//    @ApiOperation(value = "【待研究文件】文件列表", notes = "文件列表")
//    public List<String> listFile(@RequestParam("bucketName") String bucketName) {
//        return minioService.listFile(bucketName);
//    }

}
